package net.yxsoft.controller.pc.sysmgt;

import com.jfinal.kit.Kv;
import com.jfinal.kit.StrKit;
import net.yxsoft.controller.pc.sysmgt.common.AuthController;
import net.yxsoft.model.SSynDetails;
import net.yxsoft.model.SSynonyms;
import net.yxsoft.resource.SqlRes;
import net.yxsoft.service.SynonymService;
import rapid.jfinal.Pagination;

import java.sql.SQLException;

/**
 * @author 彭涛涛 2019-6-5
 */
public class SynonymController extends AuthController {

  private static final SynonymService svr = SynonymService.INSTANCE;

  public void index() {
    render("index.html");
  }

  /**
   * 获取主表(同意词)信息
   */
  public void getObjects() {
    Kv cond = Kv.create();

    String name = getPara("attrib_01");

    if(StrKit.notBlank(name)) {
      cond.set("attrib_01 like ", "%" + name + "%");
    }

    renderJson(svr.query(SqlRes.SYN_SQL + "synIndex", Kv.by("cond", cond), Pagination.by(this)));
  }

  /**
   * 获取子表(同意词集合)信息
   */
  public void getSynDetails() {

    Kv cond = Kv.create();

    String conflict_id = getPara("conflict_id");

    if(StrKit.notBlank(conflict_id)) {
      cond.set(" row_id = ", conflict_id);
    }

    renderJson(svr.query(SqlRes.SYN_SQL + "synDetails", Kv.by("cond", cond), Pagination.by(this)));
  }


  /**
   * 新增编辑主表(同意词)信息
   */
  public void saveObjects() {

    renderJson(svr.saveObjects(getModel(SSynonyms.class, "", true), getLoginUser()));
  }

  /**
   * 删除主表(同意词)信息
   */
  public void deleteObjects() throws SQLException {

    String[] ids = getPara("ids").split(",");
    renderJson(svr.deleteObjects(ids, getLoginUser()));

  }

  /**
   * 新增编辑子表(同意词集合)信息
   */
  public void saveDetailObjects() {
    renderJson(svr.saveDetailObjects(getModel(SSynDetails.class, "", true), getLoginUser()));
  }

  /**
   * 删除子表(同意词集合)信息
   */
  public void deleteDetailObjects() throws SQLException {

    String[] ids = getPara("ids").split(",");
    renderJson(svr.deleteDetailObjects(ids, getLoginUser()));

  }




}
